const app = new getApp();
var time = require("../../../../utils/util.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    prompt: '',
    default_img: {},
    order_list: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    app.defaultImg(that, true);
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  loadInfo: function() {
    let that = this;

    app.sendRequest({
      url: 'System.Member.memberInfo',
      data: {},
      success: function(res) {
        if (res.code == 0) {
          let member_info = app.checkEmpty(res.data, '');
          if (member_info != '') {
            let img = member_info.user_info.user_headimg;
            member_info.user_info.user_headimg = app.IMG(img); //图片路径处理
          }
          that.setData({
            member_info: res.data,
          })
        }
        console.log(res)
      }
    })

    that.loadPintuanList();
  },

  /**
   * 加载拼单列表
   */
  loadPintuanList: function () {
    let that = this;
    app.sendRequest({
      url: 'NsPintuan.Pintuan.pintuanOrder',
      success: function (res) {
        let code = res.code;
        let data = res.data;
        if (code == 0) {
          console.log(res);
          let order_list = app.checkEmpty(data.data, []);

          for (let index in order_list) {
            //时间格式转换
            order_list[index].create_time = time.formatTime(order_list[index].create_time, 'Y-M-D h:m:s');
            //倒计时
            let time_array = {};
            time_array.end_time = order_list[index].end_time;
            if (order_list[index].status == 1) {
              that.timing_pintuan(that, time_array, index);
            } else {
              let d = {};
              let parm = 'timers_array[' + index + ']';
              d[parm] = {};
              that.setData(d);
            }
            //用户头像处理
            let headImg = order_list[index].group_user_head_img;
            order_list[index].group_user_head_img = app.IMG(headImg);
            //商品图片处理
            let goodsImg = order_list[index].picture_info.pic_cover_micro;
            order_list[index].picture_info.pic_cover_micro = app.IMG(goodsImg);
          }

          that.setData({
            order_list: order_list
          })
        }
      }
    });
  },


  /**
   * 头像加载失败
   */
  errorHeadImg: function(e) {
    let that = this;
    let member_info = that.data.member_info;
    let default_img = that.data.default_img;
    let img = member_info.user_info.user_headimg;
    let parm = {};
    let parm_key = "member_info.user_info.user_headimg";

    if (default_img.is_use == 1) {
      default_img = default_img.value.default_headimg;
      if (img.indexOf(default_img) == -1) {
        parm[parm_key] = default_img;
        that.setData(parm);
      }
    }
  },

  /**
   * 商品图片加载失败
   */
  errorGoodsImg: function(e) {
    let that = this;
    let order_list = that.data.order_list;
    let default_img = that.data.default_img;
    let index = e.currentTarget.dataset.index;
    let img = order_list[index].picture_info.pic_cover_micro;
    let parm = {};
    let parm_key = "order_list[" + index + "].picture_info.pic_cover_micro";

    if (default_img.is_use == 1) {
      let default_img = default_img.value.default_goods_img;
      if (img.indexOf(default_img) == -1) {
        parm[parm_key] = default_img;
        that.setData(parm);
      }
    }
  },

  /**
   * 拼团计时
   */
  timing_pintuan: function (that, timer_array, index, has_day = true) {
    let current_time = that.data.current_time;
    let count_second = timer_array.end_time - current_time;
    let d = {};
    let parm = '';

    //首次加载
    if (count_second > 0) {
      count_second--;
      //时间计算
  
      let day = has_day ? Math.floor((count_second / 3600) / 24) : null;
      let hour = has_day ? Math.floor((count_second / 3600) % 24) : Math.floor(count_second / 3600);
      let minute = Math.floor((count_second / 60) % 60);
      let second = Math.floor(count_second % 60);

      hour = hour < 10 ? '0' + hour : hour;
      minute = minute < 10 ? '0' + minute : minute;
      second = second < 10 ? '0' + second : second;
      //赋值
      timer_array.day = has_day ? day : null;
      timer_array.hour = hour;
      timer_array.minute = minute;
      timer_array.second = second;
      timer_array.end = 0;

      d = {};
      parm = 'timers_array[' + index + ']';
      d[parm] = timer_array;
      that.setData(d);
    } else {
      timer_array.end = 1;

      d = {};
      parm = 'timers_array[' + index + ']';
      d[parm] = timer_array;
      that.setData(d);
    }
    //开始计时
    let timer = setInterval(function() {
      if (count_second > 0) {
        count_second--;
        //时间计算
        let day = has_day ? Math.floor((count_second / 3600) / 24) : null;
        let hour = has_day ? Math.floor((count_second / 3600) % 24) : Math.floor(count_second / 3600);
        let minute = Math.floor((count_second / 60) % 60);
        let second = Math.floor(count_second % 60);

        hour = hour < 10 ? '0' + hour : hour;
        minute = minute < 10 ? '0' + minute : minute;
        second = second < 10 ? '0' + second : second;
        //赋值
        timer_array.day = has_day ? day : null;
        timer_array.hour = hour;
        timer_array.minute = minute;
        timer_array.second = second;
        timer_array.end = 0;

        d = {};
        parm = 'timers_array[' + index + ']';
        d[parm] = timer_array;
        that.setData(d);
      } else {
        timer_array.end = 1;

        d = {};
        parm = 'timers_array[' + index + ']';
        d[parm] = timer_array;
        that.setData(d);

        clearInterval(timer);
      }
    }, 1000)
  },

})